#pragma once
#include<iostream>

using namespace std;

enum Colour
{
	RED,
	BLACK
};

template<class K,class V>
struct RBTreeNode
{
	pair<K, V> _kv;
	RBTreeNode<K, V>* _left;
	RBTreeNode<K, V>* _right;
	RBTreeNode<K, V>* _parent;
	Colour _col;


	RBTreeNode(const pair<K,V>& kv)
		:_kv(kv)
		,_left(nullptr)
		,_right(nullptr)
		,_parent(nullptr)
		,_col(RED)
	{}

};

template<class K, class V>
class RBTree
{
public:
	typedef RBTreeNode<K, V> Node;
	int _rotatecount = 0;

	bool Insert(const pair<K, V>& kv)
	{
		if (_root == nullptr)
		{
			_root = new Node(kv);
			_root->_col = BLACK;
			return true;
		}

		Node* cur = _root;
		Node* parent = nullptr;
		while (cur)
		{
			if (cur->_kv.first < kv.first)
			{
				parent = cur;
				cur = cur->_right;
			}
			else if (cur->_kv.first > kv.first)
			{
				parent = cur;
				cur = cur->_left;
			}
			else
			{
				return false;
			}
		}

		cur = new Node(kv);
		cur->_col = RED;
		if (parent->_kv.first < kv.first)
		{
			parent->_right = cur;
		}
		else
		{
			parent->_left = cur;
		}
		cur->_parent = parent;

		//调整树的平衡
		while (parent&& parent->_col == RED)
		{
			Node* grandparent = parent->_parent;
			if (grandparent->_left == parent)
			{
				Node* uncle = grandparent->_right;
				//uncle存在且为红
				if (uncle&& uncle->_col == RED)
				{
					//变色
					parent->_col = BLACK;
					uncle->_col = BLACK;
					grandparent->_col = RED;

					//继续向上调整
					cur = grandparent;
					parent = cur->_parent;
				}
				//uncle不存在 或者 存在且为黑
				else
				{
					//右单旋
					if (parent->_left == cur)
					{
						RotateR(grandparent);
						parent->_col = BLACK;
						grandparent->_col = RED;
					}
					//左右双旋
					else
					{
						RotateL(parent);
						RotateR(grandparent);

						cur->_col = BLACK;
						grandparent->_col = RED;
					}
					break;
				}
			}
			else
			{
				Node* uncle = grandparent->_left;
				//uncle存在且为红
				if (uncle&& uncle->_col == RED)
				{
					//变色
					parent->_col = BLACK;
					uncle->_col = BLACK;
					grandparent->_col = RED;

					//继续向上调整
					cur = grandparent;
					parent = cur->_parent;
				}
				//uncle不存在 或者 存在且为黑
				else
				{
					//左单旋
					if (parent->_right == cur)
					{
						RotateL(grandparent);
						parent->_col = BLACK;
						grandparent->_col = RED;

					}
					//右左双旋
					else
					{
						RotateR(parent);
						RotateL(grandparent);
						cur->_col = BLACK;
						grandparent->_col = RED;
					}
					break;
				}
			}
		}
		_root->_col = BLACK;
	}

	bool IsBalance()
	{
		return IsBalance(_root);
	}

	bool IsBalance(Node* root)
	{
		if (root == nullptr)
			return true;

		//连续红节点
		if (root->_col == RED && root->_parent && root->_parent->_col == RED)
		{
			cout << root->_kv.first << ":有连续红节点" << endl;
			return false;
		}

		//基准值
		Node* cur = root;
		int bechmark = 0;
		while (cur)
		{
			if (cur->_col == BLACK)
			    bechmark++;

			cur = cur->_left;
		}

		return checkcolour(root, 0, bechmark);
	}

	bool checkcolour(Node* root, int blacknum, int bechmark)
	{
		if (root == nullptr)
		{
			if (blacknum != bechmark)
			{
				return false;
			}

			return true;
		}

		if (root->_col == BLACK)
			blacknum++;

		return checkcolour(root->_left, blacknum, bechmark) && checkcolour(root->_right, blacknum, bechmark);
	}

	int Height()
	{
		return Height(_root);
	}

	int Height(Node* root)
	{
		if (root == nullptr)
			return 0;

		int left = Height(root->_left);
		int right = Height(root->_right);

		return left > right ? left + 1 : right + 1;
	}
	
private:
	void RotateL(Node* parent)
	{
		_rotatecount++;

		Node* cur = parent->_right;
		Node* curleft = cur->_left;

		parent->_right = curleft;
		if (curleft)
			curleft->_parent = parent;

		cur->_left = parent;
		Node* ppnode = parent->_parent;
		parent->_parent = cur;

		if (parent == _root)
		{
			_root = cur;
			cur->_parent = nullptr;
		}
		else
		{
			if (ppnode->_right == parent)
			{
				cur->_parent = ppnode;
				ppnode->_right = cur;
			}
			else
			{
				cur->_parent = ppnode;
				ppnode->_left = cur;
			}
		}
	}

	void RotateR(Node* parent)
	{
		_rotatecount++;

		Node* cur = parent->_left;
		Node* curright = cur->_right;

		parent->_left = curright;
		if (curright)
			curright->_parent = parent;

		cur->_right = parent;
		Node* ppnode = parent->_parent;
		parent->_parent = cur;

		if (parent == _root)
		{
			_root = cur;
			cur->_parent = nullptr;
		}

		else
		{
			if (ppnode->_left == parent)
			{
				ppnode->_left = cur;
			}
			else
			{
				ppnode->_right = cur;
			}
			cur->_parent = ppnode;
		}

	}
	Node* _root = nullptr;
};